package cn.zyq.demo.service.sys.impl;


import cn.zyq.demo.beans.sys.SysAdmin;
import cn.zyq.demo.beans.sys.SysOperLog;
import cn.zyq.demo.mapper.sys.SysOperLogMapper;
import cn.zyq.demo.service.sys.SysOperLogService;
import cn.zyq.demo.system.common.BaseService;
import cn.zyq.demo.system.common.PageParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作日志记录
 * 2023-05-12 20:14:09
 */

@Slf4j
@Service
public class SysOperLogServiceImpl extends BaseService implements SysOperLogService {
    @Autowired
    private SysOperLogMapper sysOperLogMapper;


    @Override
    public PageInfo<SysOperLog> list(PageParam pageParam, SysAdmin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SysOperLog> dbList = this.sysOperLogMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(SysOperLog param, SysAdmin admin) {
        this.setCreateBaseData(param, admin);
        this.sysOperLogMapper.insertSelective(param);
    }

    @Override
    public void update(SysOperLog param, SysAdmin admin) {
        Integer id = param.getSysOperLogID();
        this.setUpdateBaseData(param, admin);
        this.sysOperLogMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, SysAdmin admin) {
        SysOperLog dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getSysOperLogID();
        this.deleteLogic(id, admin, this.sysOperLogMapper);
    }

    @Override
    public SysOperLog detail(String serviceID, SysAdmin admin) {
        return this.detail(serviceID);
    }

    @Override
    public SysOperLog detail(String serviceID) {
        SysOperLog dbInfo = this.getByServiceID(serviceID, SysOperLog.class, this.sysOperLogMapper);
        this.checkLogicExist(dbInfo);
        return dbInfo;
    }

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        sysOperLogMapper.insertSelective(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(PageParam pageParam)
    {
        return sysOperLogMapper.selectOperLogList(pageParam.getKv());
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Integer[] operIds)
    {
        int i = 0;
        for (Integer infoId : operIds) {
            i += sysOperLogMapper.deleteByPrimaryKey(infoId);
        }
        return i;
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Integer operId)
    {
        return sysOperLogMapper.selectByPrimaryKey(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        sysOperLogMapper.cleanOperLog();
    }
}
